#include <nhttpd/detect.h>
#include <nhttpd/queue.h>
#include <nhttpd/log.h>
#include <nhttpd/error.h>

void detect_init() {
#if NH_THREADS == 0
  NHD_THREADS = sysconf(_SC_THREADS) < 0;
#endif
#if NH_SYSLOG == 0
  NHD_SYSLOG = sysconf(_SC_XOPEN_UNIX) < 0;
#endif
#if NH_XMQ == 0
  NHD_XMQ = sysconf(_SC_XOPEN_UNIX) < 0;
#endif
#if NH_PMQ == 0
  NHD_PMQ = sysconf(_SC_MESSAGE_PASSING) < 0;
#endif
#if NH_TIMEOUTS == 0
	NHD_TIMEOUTS = sysconf(_SC_TIMEOUTS);
#endif
NHQ_init();
NH_Log_init();
NHE_init();
}

#ifndef NH_ASPRINTF
#include <stdlib.h>

int asprintf(char** strp, const char* fmt, ...) {
  va_list args;
  va_start(args, fmt);
  int sz = vasprintf(strp, fmt, args);
  va_end(args);
  return sz;
}

int vasprintf(char** strp, const char* fmt, va_list args) {
  char* buf = calloc(NH_ASPRINTF_BUF, sizeof(char));
	int sz = vsnprintf(buf, NH_ASPRINTF_BUF, fmt, args);
  if (sz >= NH_ASPRINTF_BUF) {
	 realloc(buf, sz * sizeof(char));
	 sz = vsnprintf(buf, sz, fmt, args);
  }
  *strp = buf;
  return sz;
}
#endif
